package com.qs.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.qs.constant.PayTypeConstant;
import com.qs.exception.ServiceException;
import com.qs.pojo.entity.Pay;
import com.qs.response.ResponseEnum;
import com.qs.service.IAsyncService;
import com.qs.service.IOrderPayService;
import com.qs.utils.GenerateQRcodeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: OrderPayServiceImpl
 * Package: com.qs.service.impl
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/12/31 - 11:01
 * @Version: v1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderPayServiceImpl implements IOrderPayService {

    private final IAsyncService asyncService;

    private final AlipayConfig alipayConfig;

    private final Object lock = new Object();

    private static final String TRADE_SUCCESS = "TRADE_SUCCESS";

    @Override
    public boolean queryOrder(String orderId, HttpServletRequest res) {
        synchronized(lock) {
            boolean isSuccess = false;

            try {
                AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
                AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
                AlipayTradeQueryModel model = new AlipayTradeQueryModel();

                model.setOutTradeNo(orderId);
                request.setBizModel(model);
                AlipayTradeQueryResponse response = alipayClient.execute(request);
                if (response.isSuccess()) {
                    if (TRADE_SUCCESS.equalsIgnoreCase(response.getTradeStatus())) {
                        log.info("订单号为：{}的订单支付成功", orderId);
                        isSuccess = true;
                        // 异步更新订单状态
                        asyncService.updatePayInfo(orderId, PayTypeConstant.ALI_PAY);

                        return isSuccess;
                    } else {
                        log.info("订单号为：{}的订单支付失败，状态为：{}", orderId, response.getTradeStatus());
                        return isSuccess;
                    }
                } else {
                    log.info("订单号为：{}的查询请求失败", orderId);
                    return isSuccess;
                }
            } catch (AlipayApiException e) {
                log.error("支付宝发起支付请求异常", e);
                throw new RuntimeException("处理支付宝请求时发生异常", e);
            } finally {
                lock.notifyAll();
            }
        }
    }
}
